<?php

  session_start();
  include "config.php";
  include "ALproduct.php";
  include "ALcustomer.php";
  include "ALorder.php";

  date_default_timezone_set("Australia/Sydney");

  if(isset($_SESSION["user"]))
  {
    if(!isset($_SESSION["o_confirmed"]))
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  window.location.href = 'orderProducts.php';";
      echo "</SCRIPT>";
    }
 
    include "$header";
	include "cardExpiryDate.js";

?>

<SCRIPT language='Javascript'>

  function getDetails(inputName, product, detailsSize)
  {
    var i;
    var inputNo;
    var val;

    if(inputName == 'product0')
    {
      val = getValue(0);
      inputNo = 0;
    }
    else if(inputName == 'product1')
    {
      val = getValue(1);
      inputNo = 1;
    }
    else if(inputName == 'product2')
    {
      val = getValue(2);
      inputNo = 2;
    }
    else if(inputName == 'product3')
    {
      val = getValue(3);
      inputNo = 3;
    }
    else if(inputName == 'product4')
    {
      val = getValue(4);
      inputNo = 4;
    }
    else if(inputName == 'product5')
    {
      val = getValue(5);
      inputNo = 5;
    }
    else if(inputName == 'product6')
    {
      val = getValue(6);
      inputNo = 6;
    }
    else if(inputName == 'product7')
    {
      val = getValue(7);
      inputNo = 7;
    }
    else if(inputName == 'product8')
    {
      val = getValue(8);
      inputNo = 8;
    }
    else if(inputName == 'product9')
    {
      val = getValue(9);
      inputNo = 9;
    }

    val = trimString(val);

    if(val == "")
    {
      clearValues(inputNo);
      disableQty(inputNo);
    }
    else
    {
      if(validProductEntry(val))
      {
        for(i=0; i < detailsSize; i++)
        {
          if(val == product[i][0])
          {
            var stockLevel = getStockLevel(val, product, detailsSize);

            if(stockLevel == 0)
            {
              alert('The product [' + val + '] currently has no stock.  Please enter another product no.');
              clearValues(inputNo);
              break;
            }
            else
            {
              setValues(inputNo, val, product[i][1], product[i][2], product[i][3], product[i][4]);
              enableQty(inputNo);
              setDefaultQty(inputNo);
              break;
            }
          }
          else if(i == (product.length - 1))
          {
            alert('The product no. [' + val + '] does not exist.  Please re-enter the product no.');
            clearValues(inputNo);
            disableQty(inputNo);
          }
        }
      }
      else
      {
        alert('The product no. [' + val + '] already exists in the order.  Please re-enter the product no.');
        clearValues(inputNo);
        disableQty(inputNo);
      }
    }
  }

  function trimString (str)
  {
    while (str.charAt(0) == ' ')
      str = str.substring(1);
    while (str.charAt(str.length - 1) == ' ')
      str = str.substring(0, str.length - 1);
    return str;
  }

  function disableQty(num)
  {
    if(num == 0)
      orderForm.product0_qty.disabled = true;
    else if(num == 1)
      orderForm.product1_qty.disabled = true;
    else if(num == 2)
      orderForm.product2_qty.disabled = true;
    else if(num == 3)
      orderForm.product3_qty.disabled = true;
    else if(num == 4)
      orderForm.product4_qty.disabled = true;
    else if(num == 5)
      orderForm.product5_qty.disabled = true;
    else if(num == 6)
      orderForm.product6_qty.disabled = true;
    else if(num == 7)
      orderForm.product7_qty.disabled = true;
    else if(num == 8)
      orderForm.product8_qty.disabled = true;
    else if(num == 9)
      orderForm.product9_qty.disabled = true;
  }

  function enableQty(num)
  {
    if(num == 0)
      orderForm.product0_qty.disabled = false;
    else if(num == 1)
      orderForm.product1_qty.disabled = false;
    else if(num == 2)
      orderForm.product2_qty.disabled = false;
    else if(num == 3)
      orderForm.product3_qty.disabled = false;
    else if(num == 4)
      orderForm.product4_qty.disabled = false;
    else if(num == 5)
      orderForm.product5_qty.disabled = false;
    else if(num == 6)
      orderForm.product6_qty.disabled = false;
    else if(num == 7)
      orderForm.product7_qty.disabled = false;
    else if(num == 8)
      orderForm.product8_qty.disabled = false;
    else if(num == 9)
      orderForm.product9_qty.disabled = false;
  }

  function validProductEntry(val)
  {
    var i = 0;

    if(orderForm.product0.value == val)
    {
      i++;
    }
    if(orderForm.product1.value == val)
    {
      i++;
    }
    if(orderForm.product2.value == val)
    {
      i++;
    }
    if(orderForm.product3.value == val)
    {
      i++;
    }
    if(orderForm.product4.value == val)
    {
      i++;
    }
    if(orderForm.product5.value == val)
    {
      i++;
    }
    if(orderForm.product6.value == val)
    {
      i++;
    }
    if(orderForm.product7.value == val)
    {
      i++;
    }
    if(orderForm.product8.value == val)
    {
      i++;
    }
    if(orderForm.product9.value == val)
    {
      i++;
    }

    if(i == 1)
      return true;
	else
      return false;

  }

  function getValue(num)
  {
    if(num == 0)
      return orderForm.product0.value;
    else if(num == 1)
      return orderForm.product1.value;
    else if(num == 2)
      return orderForm.product2.value;
    else if(num == 3)
      return orderForm.product3.value;
    else if(num == 4)
      return orderForm.product4.value;
    else if(num == 5)
      return orderForm.product5.value;
    else if(num == 6)
      return orderForm.product6.value;
    else if(num == 7)
      return orderForm.product7.value;
    else if(num == 8)
      return orderForm.product8.value;
    else if(num == 9)
      return orderForm.product9.value;
  }

  function setValues(num, val, colour, size, des, item)
  {
    if(num == 0)
    {
      orderForm.product0.value = val;
      orderForm.product0_qty.value = "";
      orderForm.product0_colour.value = colour;
      orderForm.product0_size.value = size;
      orderForm.product0_des.value = des;
      orderForm.product0_item.value = item;
    }
    else if(num == 1)
    {
      orderForm.product1.value = val;
      orderForm.product1_qty.value = "";
      orderForm.product1_colour.value = colour;
      orderForm.product1_size.value = size;
      orderForm.product1_des.value = des;
      orderForm.product1_item.value = item;
    }
    else if(num == 2)
    {
      orderForm.product2.value = val;
      orderForm.product2_qty.value = "";
      orderForm.product2_colour.value = colour;
      orderForm.product2_size.value = size;
      orderForm.product2_des.value = des;
      orderForm.product2_item.value = item;
    }
    else if(num == 3)
    {
      orderForm.product3.value = val;
      orderForm.product3_qty.value = "";
      orderForm.product3_colour.value = colour;
      orderForm.product3_size.value = size;
      orderForm.product3_des.value = des;
      orderForm.product3_item.value = item;
    }
    else if(num == 4)
    {
      orderForm.product4.value = val;
      orderForm.product4_qty.value = "";
      orderForm.product4_colour.value = colour;
      orderForm.product4_size.value = size;
      orderForm.product4_des.value = des;
      orderForm.product4_item.value = item;
    }
    else if(num == 5)
    {
      orderForm.product5.value = val;
      orderForm.product5_qty.value = "";
      orderForm.product5_colour.value = colour;
      orderForm.product5_size.value = size;
      orderForm.product5_des.value = des;
      orderForm.product5_item.value = item;
    }
    else if(num == 6)
    {
      orderForm.product6.value = val;
      orderForm.product6_qty.value = "";
      orderForm.product6_colour.value = colour;
      orderForm.product6_size.value = size;
      orderForm.product6_des.value = des;
      orderForm.product6_item.value = item;
    }
    else if(num == 7)
    {
      orderForm.product7.value = val;
      orderForm.product7_qty.value = "";
      orderForm.product7_colour.value = colour;
      orderForm.product7_size.value = size;
      orderForm.product7_des.value = des;
      orderForm.product7_item.value = item;
    }
    else if(num == 8)
    {
      orderForm.product8.value = val;
      orderForm.product8_qty.value = "";
      orderForm.product8_colour.value = colour;
      orderForm.product8_size.value = size;
      orderForm.product8_des.value = des;
      orderForm.product8_item.value = item;
    }
    else if(num == 9)
    {
      orderForm.product9.value = val;
      orderForm.product9_qty.value = "";
      orderForm.product9_colour.value = colour;
      orderForm.product9_size.value = size;
      orderForm.product9_des.value = des;
      orderForm.product9_item.value = item;
    }
  }

  function clearValues(num)
  {
    if(num == 0)
    {
      orderForm.product0.value = "";
      orderForm.product0_qty.value = "";
      orderForm.product0_colour.value = "";
      orderForm.product0_size.value = "";
      orderForm.product0_des.value = "";
      orderForm.product0_item.value = "";
      orderForm.product0_total.value = "";
    }
    else if(num == 1)
    {
      orderForm.product1.value = "";
      orderForm.product1_qty.value = "";
      orderForm.product1_colour.value = "";
      orderForm.product1_size.value = "";
      orderForm.product1_des.value = "";
      orderForm.product1_item.value = "";
      orderForm.product1_total.value = "";
    }
    else if(num == 2)
    {
      orderForm.product2.value = "";
      orderForm.product2_qty.value = "";
      orderForm.product2_colour.value = "";
      orderForm.product2_size.value = "";
      orderForm.product2_des.value = "";
      orderForm.product2_item.value = "";
      orderForm.product2_total.value = "";	 
    }
    else if(num == 3)
    {
      orderForm.product3.value = "";
      orderForm.product3_qty.value = "";
      orderForm.product3_colour.value = "";
      orderForm.product3_size.value = "";
      orderForm.product3_des.value = "";
      orderForm.product3_item.value = "";
      orderForm.product3_total.value = "";
    }
    else if(num == 4)
    {
      orderForm.product4.value = "";
      orderForm.product4_qty.value = "";
      orderForm.product4_colour.value = "";
      orderForm.product4_size.value = "";
      orderForm.product4_des.value = "";
      orderForm.product4_item.value = "";
      orderForm.product4_total.value = "";	 
    }
    else if(num == 5)
    {
      orderForm.product5.value = "";
      orderForm.product5_qty.value = "";
      orderForm.product5_colour.value = "";
      orderForm.product5_size.value = "";
      orderForm.product5_des.value = ""
      orderForm.product5_item.value = "";
      orderForm.product5_total.value = "";	
    }
    else if(num == 6)
    {
      orderForm.product6.value = "";
      orderForm.product6_qty.value = "";
      orderForm.product6_colour.value = "";
      orderForm.product6_size.value = "";
      orderForm.product6_des.value = "";
      orderForm.product6_item.value = "";
      orderForm.product6_total.value = "";
    }
    else if(num == 7)
    {
      orderForm.product7.value = "";
      orderForm.product7_qty.value = "";
      orderForm.product7_colour.value = "";
      orderForm.product7_size.value = "";
      orderForm.product7_des.value = "";
      orderForm.product7_item.value = "";
      orderForm.product7_total.value = "";	 
    }
    else if(num == 8)
    {
      orderForm.product8.value = "";
      orderForm.product8_qty.value = "";
      orderForm.product8_colour.value = "";
      orderForm.product8_size.value = "";
      orderForm.product8_des.value = "";
      orderForm.product8_item.value = "";
      orderForm.product8_total.value = "";	 
    }
    else if(num == 9)
    {
      orderForm.product9.value = "";
      orderForm.product9_qty.value = "";
      orderForm.product9_colour.value = "";
      orderForm.product9_size.value = "";
      orderForm.product9_des.value = "";
      orderForm.product9_item.value = "";
      orderForm.product9_total.value = "";	 
    }
    getMerchanisePrice();
  }

  function getTotalPrice(inputName, val, product, detailsSize)
  {
    val = trimString(val);
    var inputNo;
    var productNo;
    var stockLevel;

    if(inputName == 'product0_qty')
    {
      productNo = getValue(0);
      inputNo = 0;
    }
    else if(inputName == 'product1_qty')
    {
      productNo = getValue(1);
      inputNo = 1;
    }
    else if(inputName == 'product2_qty')
    {
      productNo = getValue(2);
      inputNo = 2;
    }
    else if(inputName == 'product3_qty')
    {
      productNo = getValue(3);
      inputNo = 3;
    }
    else if(inputName == 'product4_qty')
    {
      productNo = getValue(4);
      inputNo = 4;
    }
    else if(inputName == 'product5_qty')
    {
      productNo = getValue(5);
      inputNo = 5;
    }
    else if(inputName == 'product6_qty')
    {
      productNo = getValue(6);
      inputNo = 6;
    }
    else if(inputName == 'product7_qty')
    {
      productNo = getValue(7);
      inputNo = 7;
    }
    else if(inputName == 'product8_qty')
    {
      productNo = getValue(8);
      inputNo = 8;
    }
    else if(inputName == 'product9_qty')
    {
      productNo = getValue(9);
      inputNo = 9;
    }

    productNo = trimString(productNo);

    if(isNumeric(val))
    {
      stockLevel = getStockLevel(productNo, product, detailsSize);

      if(stockLevel == 0)
      {
        alert('The product [' + productNo + '] currently has no stock.  Please enter another product no.');
        clearValues(inputNo);
      }
      else if(val == 0)
      {
        alert('The quantity for the product no. [' + productNo + '] must be more than 0.');
        setDefaultQty(inputNo);
      }
      else if(val > stockLevel)
      {
        alert('The quantity for the product no. [' + productNo + '] must be equal to or less than ' + stockLevel + '.');
        setDefaultQty(inputNo);
      }
      else
      {
        setTotalPrice(inputNo, val);
      }
    }
    else
    {
      alert('The quantity for the product no. [' + productNo + '] must be numeric.  Please re-enter the value.');
      setDefaultQty(inputNo);
    }
  }

  function getStockLevel(productNo, product, detailsSize)
  {
    var i;
    for(i=0; i < detailsSize; i++)
    {
      if(product[i][0] == productNo)
      {
        return product[i][5];
      }
    }
  }

  function setTotalPrice(num, val)
  {
    if(num == 0)
      orderForm.product0_total.value = (val * orderForm.product0_item.value).toFixed(2);
    else if(num == 1)
      orderForm.product1_total.value = (val * orderForm.product1_item.value).toFixed(2);
    else if(num == 2)
      orderForm.product2_total.value = (val * orderForm.product2_item.value).toFixed(2);
    else if(num == 3)
      orderForm.product3_total.value = (val * orderForm.product3_item.value).toFixed(2);
    else if(num == 4)
      orderForm.product4_total.value = (val * orderForm.product4_item.value).toFixed(2);
    else if(num == 5)
      orderForm.product5_total.value = (val * orderForm.product5_item.value).toFixed(2);
    else if(num == 6)
      orderForm.product6_total.value = (val * orderForm.product6_item.value).toFixed(2);
    else if(num == 7)
      orderForm.product7_total.value = (val * orderForm.product7_item.value).toFixed(2);
    else if(num == 8)
      orderForm.product8_total.value = (val * orderForm.product8_item.value).toFixed(2);
    else if(num == 9)
      orderForm.product9_total.value = (val * orderForm.product9_item.value).toFixed(2);
  }

  function setDefaultQty(num)
  {
    if(num == 0)
    {
      orderForm.product0_qty.value = "1";
      setTotalPrice(0, "1");
    }
    else if(num == 1)
    {
      orderForm.product1_qty.value = "1";
      setTotalPrice(1, "1");
    }
    else if(num == 2)
    {
      orderForm.product2_qty.value = "1";
      setTotalPrice(2, "1");
    }
    else if(num == 3)
    {
      orderForm.product3_qty.value = "1";
      setTotalPrice(3, "1");
    }
    else if(num == 4)
    {
      orderForm.product4_qty.value = "1";
      setTotalPrice(4, "1");
    }
    else if(num == 5)
    {
      orderForm.product5_qty.value = "1";
      setTotalPrice(5, "1");
    }
    else if(num == 6)
    {
      orderForm.product6_qty.value = "1";
      setTotalPrice(6, "1");
    }
    else if(num == 7)
    {
      orderForm.product7_qty.value = "1";
      setTotalPrice(7, "1");
    }
    else if(num == 8)
    {
      orderForm.product8_qty.value = "1";
      setTotalPrice(8, "1");
    }
    else if(num == 9)
    {
      orderForm.product9_qty.value = "1";
      setTotalPrice(9, "1");
    }
  }

  function isNumeric(str)
  {
    var i;
 
    for (i=0; i < str.length; i++) 
    { 
      if (str.charAt(i) != "0" && str.charAt(i) != "1" && str.charAt(i) != "2" && str.charAt(i) != "3" && str.charAt(i) != "4" && str.charAt(i) != "5" && str.charAt(i) != "6" && str.charAt(i) != "7" && str.charAt(i) != "8" && str.charAt(i) != "9") 
      {
        return false;
      }
    }
    return true;
  }
  
  function getMerchanisePrice()
  {
    p0 = orderForm.product0_total.value;
    p1 = orderForm.product1_total.value;
    p2 = orderForm.product2_total.value;
    p3 = orderForm.product3_total.value;
    p4 = orderForm.product4_total.value;
    p5 = orderForm.product5_total.value;
    p6 = orderForm.product6_total.value;
    p7 = orderForm.product7_total.value;
    p8 = orderForm.product8_total.value;
    p9 = orderForm.product9_total.value;
    
    if(p0 == "")
      p0 = 0;
    if(p1 == "")
      p1 = 0;
    if(p2 == "")
      p2 = 0;
    if(p3 == "")
      p3 = 0;
    if(p4 == "")
      p4 = 0;
    if(p5 == "")
      p5 = 0;
    if(p6 == "")
      p6 = 0;
    if(p7 == "")
      p7 = 0;
    if(p8 == "")
      p8 = 0;
    if(p9 == "")
      p9 = 0;

    orderForm.merchanise.value = (parseFloat(p0) + parseFloat(p1) + parseFloat(p2) + parseFloat(p3) + parseFloat(p4) + parseFloat(p5) + parseFloat(p6) + parseFloat(p7) + parseFloat(p8) + parseFloat(p9)).toFixed(2);

    getDeliveryPrice();
    getInsurancePrice();
    setOrderTotalPrice();
  }

  function getDeliveryPrice()
  {
    if(orderForm.freight.value == "post")
    {
      if(orderForm.merchanise.value >= 0 && orderForm.merchanise.value <= 50)
      {
        orderForm.delivery.value = (4.00).toFixed(2);
      }
      else if(orderForm.merchanise.value > 50 && orderForm.merchanise.value <= 100)
      {
        orderForm.delivery.value = (7.50).toFixed(2);
      }
      else if(orderForm.merchanise.value > 100 && orderForm.merchanise.value <= 500)
      {
        orderForm.delivery.value = (9.00).toFixed(2);
      }
      else if(orderForm.merchanise.value > 500)
      {
        orderForm.delivery.value = (11.00).toFixed(2);
      }
    }
    else if(orderForm.freight.value == "road")
    {
      if(orderForm.merchanise.value >= 0 && orderForm.merchanise.value <= 50)
      {
        orderForm.delivery.value = (7.50).toFixed(2);
      }
      else if(orderForm.merchanise.value > 50 && orderForm.merchanise.value <= 100)
      {
        orderForm.delivery.value = (9.50).toFixed(2);
      }
      else if(orderForm.merchanise.value > 100 && orderForm.merchanise.value <= 500)
      {
        orderForm.delivery.value = (12.00).toFixed(2);
      }
      else if(orderForm.merchanise.value > 500)
      {
        orderForm.delivery.value = (15.00).toFixed(2);
      }
    }
    else if(orderForm.freight.value == "air")
    {
      if(orderForm.merchanise.value >= 0 && orderForm.merchanise.value <= 50)
      {
        orderForm.delivery.value = (8.00).toFixed(2);
      }
      else if(orderForm.merchanise.value > 50 && orderForm.merchanise.value <= 100)
      {
        orderForm.delivery.value = (25.00).toFixed(2);
      }
      else if(orderForm.merchanise.value > 100 && orderForm.merchanise.value <= 500)
      {
        orderForm.delivery.value = (50.00).toFixed(2);
      }
      else if(orderForm.merchanise.value > 500)
      {
        orderForm.delivery.value = (80.00).toFixed(2);
      }
    }
    else
    {
      orderForm.delivery.value = (0).toFixed(2);
    }
    
    setOrderTotalPrice();
  }
  
  function getInsurancePrice()
  {
    orderForm.insurance.value = (Math.ceil(orderForm.merchanise.value / 100)).toFixed(2);
    
    setOrderTotalPrice();
  }
  
  function setOrderTotalPrice()
  {

    orderForm.total.value = (parseFloat(orderForm.merchanise.value) + parseFloat(orderForm.delivery.value) + parseFloat(orderForm.insurance.value)).toFixed(2);

  }

</SCRIPT>

<?php

    include "$body";

    $details = findProductDetails();
    $stockLevels = findStockLevels();

	$_SESSION["o_productDetails"] = $details;

    if($details == "0" || $stockLevels == "0")
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  alert('There are no products available for ordering.');";
      echo "  window.location.href = 'main.php';";
      echo "</SCRIPT>";
    }

    $detailsSize = sizeof($details);
    $stockLevelsSize = sizeof($stockLevels);

    echo "\n<SCRIPT language='Javascript'>\n";
    echo "  var detailsSize = '$detailsSize';";
    echo "  var product = new Array();\n";
    for($i=0; $i < sizeof($details); $i++)
    {
      echo "  product[$i] = new Array(5);\n";
      $product_row = $details[$i];
      echo "  product[$i][0] = '$product_row[0]';\n";
      echo "  product[$i][1] = '$product_row[1]';\n";
      echo "  product[$i][2] = '$product_row[2]';\n";
      echo "  product[$i][3] = '$product_row[3]';\n";
      echo "  product[$i][4] = '$product_row[4]';\n";
      echo "  product[$i][5] = '';\n";
      for($j=0; $j < sizeof($stockLevels); $j++)
      {
        $prod = $stockLevels[$j][0];
        $stock = $stockLevels[$j][1];
        echo "  if(product[$i][0] == '$prod')";
        echo "  {";
        echo "    product[$i][5] = '$stock';";
        echo "  }";
      }
      echo "  if(product[$i][5] == '')";
      echo "  {";
      echo "    product[$i][5] = '0';";
      echo "  }";
    }
    echo "</SCRIPT>\n";

?>

<div style="position:absolute; top:0px; left:1px; border:0;">
  <span class="prevPage">Order Products:Search > </span>
  <span class="prevPage">Confirm Customer Details > </span>
  <span class="page">Order Products</span>
</div>
  
<?php

    include "$H_op";

    if(isset($_POST["submit"]))
    {
      $o_product[0] = ltrim($_POST["product0"]);
      $o_product[1] = ltrim($_POST["product0_qty"]);
      $o_product[2] = ltrim($_POST["product1"]);
      $o_product[3] = ltrim($_POST["product1_qty"]);
      $o_product[4] = ltrim($_POST["product2"]);
      $o_product[5] = ltrim($_POST["product2_qty"]);
      $o_product[6] = ltrim($_POST["product3"]);
      $o_product[7] = ltrim($_POST["product3_qty"]);
      $o_product[8] = ltrim($_POST["product4"]);
      $o_product[9] = ltrim($_POST["product4_qty"]);
      $o_product[10] = ltrim($_POST["product5"]);
      $o_product[11] = ltrim($_POST["product5_qty"]);
      $o_product[12] = ltrim($_POST["product6"]);
      $o_product[13] = ltrim($_POST["product6_qty"]);
      $o_product[14] = ltrim($_POST["product7"]);
      $o_product[15] = ltrim($_POST["product7_qty"]);
      $o_product[16] = ltrim($_POST["product8"]);
      $o_product[17] = ltrim($_POST["product8_qty"]);
      $o_product[18] = ltrim($_POST["product9"]);
      $o_product[19] = ltrim($_POST["product9_qty"]);

      $freight = $_POST["freight"];
      $merchanise = $_POST["merchanise"];
      $delivery = $_POST["delivery"];
      $insurance = $_POST["insurance"];
      $total = $_POST["total"];

      $payment = $_POST["payment"];

      if($payment == "preferred")
      {
        $card_details = getPreferredCredit($_SESSION["c_no"]);
      }
      else if($payment == "alternate")
      {
        $card_details = getAlternateCredit($_SESSION["c_no"]);
      }
      else if($payment == "other")
      {
        $card_details[0] = $_POST["card_type"];
        $card_details[1]= $_POST["card_name"];
        $card_details[2] = $_POST["card_no_1"];
        $card_details[3] = $_POST["card_no_2"];
        $card_details[4] = $_POST["card_no_3"];
        $card_details[5] = $_POST["card_no_4"];
        $card_details[6] = $_POST["card_expiry_month"];
        $card_details[7] = substr($_POST["card_expiry_year"],2);
      }
      
      $validate = validateOrderDetails($o_product, $freight, $merchanise, $delivery, $insurance, $total, $card_details);
      
      if($validate == "0")
      {
        showForm();
        $j = 0;
        
        echo "<SCRIPT language='Javascript'>\n";
        for($i=0; $i < sizeof($o_product); $i+=2)
        {
          $qty = $o_product[$i+1];

          if($o_product[$i] == "" || empty($o_product[$i]))
          {
            echo "  orderForm.product" . $j . ".value = '';\n";
            echo "  orderForm.product" . $j . "_qty.value = '';\n";
            echo "  orderForm.product" . $j . "_colour.value = '';\n";
            echo "  orderForm.product" . $j . "_size.value = '';\n";
            echo "  orderForm.product" . $j . "_des.value = '';\n";
            echo "  orderForm.product" . $j . "_item.value = '';\n";
            echo "  orderForm.product" . $j . "_total.value = '';\n";
            echo "  orderForm.product" . $j . "_qty.disabled = true;\n";
          }
          else
          {
            echo "  orderForm.product" . $j . ".value='$o_product[$i]';\n";
            echo "  for(var j=0; j < product.length; j++)\n";
            echo "  {\n";
            echo "    if(product[j][0] == '$o_product[$i]')\n";
            echo "    {\n";
            echo "      orderForm.product" . $j . "_qty.value = '$qty';\n";
            echo "      orderForm.product" . $j . "_colour.value = product[j][1];\n";
            echo "      orderForm.product" . $j . "_size.value = product[j][2];\n";
            echo "      orderForm.product" . $j . "_des.value = product[j][3];\n";
            echo "      orderForm.product" . $j . "_item.value = product[j][4];\n";
            echo "      orderForm.product" . $j . "_total.value = ('$qty' * product[j][4]).toFixed(2);\n";
            echo "      orderForm.product" . $j . "_qty.disabled = false;\n";
            echo "      break;\n";
            echo "    }\n";
            echo "    else if(j == product.length - 1)\n";
            echo "    {";
            echo "      alert('The product no. $o_product[$i] does not exist.  Please re-enter the product no.');\n";
            echo "    }\n";
            echo "  }\n";
          }
          $j++;
        }

        echo "  orderForm.freight.value = '" . $_POST["freight"] . "';";
        echo "  orderForm.merchanise.value = '" . $_POST["merchanise"] . "';";
        echo "  orderForm.delivery.value = '" . $_POST["delivery"] . "';";
        echo "  orderForm.insurance.value = '" . $_POST["insurance"] . "';";
        echo "  orderForm.total.value = '" . $_POST["total"] . "';";

        if($payment == "preferred")
        {
          echo "  orderForm.payment[0].checked = true;";
          echo "  orderForm.card_type.value = 'Select:';";
          echo "  orderForm.card_name.value = '';";
          echo "  orderForm.card_no_1.value = '';";
          echo "  orderForm.card_no_2.value = '';";
          echo "  orderForm.card_no_3.value = '';";
          echo "  orderForm.card_no_4.value = '';";
          echo "  orderForm.card_expiry_month.selectedIndex = 0;";
          echo "  orderForm.card_expiry_year.selectedIndex = 0;";
        }
        else if($payment == "alternate")
        {
          echo "  orderForm.payment[1].checked = true;";
          echo "  orderForm.card_type.value = 'Select:';";
          echo "  orderForm.card_name.value = '';";
          echo "  orderForm.card_no_1.value = '';";
          echo "  orderForm.card_no_2.value = '';";
          echo "  orderForm.card_no_3.value = '';";
          echo "  orderForm.card_no_4.value = '';";
          echo "  orderForm.card_expiry_month.selectedIndex = 0;";
          echo "  orderForm.card_expiry_year.selectedIndex = 0;";
        }
        else if($payment == "other")
        {
          echo "  orderForm.payment[2].checked = true;";
          echo "  orderForm.card_type.value = '" . $_POST["card_type"] . "';";
          echo "  orderForm.card_name.value = '" . $_POST["card_name"] . "';";
          echo "  orderForm.card_no_1.value = '" . $_POST["card_no_1"] . "';";
          echo "  orderForm.card_no_2.value = '" . $_POST["card_no_2"] . "';";
          echo "  orderForm.card_no_3.value = '" . $_POST["card_no_3"] . "';";
          echo "  orderForm.card_no_4.value = '" . $_POST["card_no_4"] . "';";
          echo "  orderForm.card_expiry_month.value = '" . $_POST["card_expiry_month"] . "';";
          echo "  orderForm.card_expiry_year.value = '" . $_POST["card_expiry_year"] . "';";
        }
        
        echo "</SCRIPT>\n";
      }
      else
      {
        $_SESSION["o_products"] = $validate;
        $_SESSION["o_freight"] = $freight;
        $_SESSION["o_merchanise"] = $merchanise;
        $_SESSION["o_delivery"] = $delivery;
        $_SESSION["o_insurance"] = $insurance;
        $_SESSION["o_total"] = $total;
        $_SESSION["o_payment"] = $payment;
        $_SESSION["o_card"] = $card_details;
        echo "<SCRIPT language='Javascript'>";
        echo "  window.location.href = 'orderProducts_3confirmOrder.php';";
        echo "</SCRIPT>";
      }
    }
    else
    {
      showForm();

      if(isset($_SESSION["o_products"]))
      {
        $o_products = $_SESSION["o_products"];

        $j = 0;

        echo "<SCRIPT language='Javascript'>\n";
        for($i=0; $i < sizeof($o_products); $i+=2)
        {
          $qty = $o_products[$i+1];
          echo "orderForm.product" . $j . ".value='" . $o_products[$i] . "';\n";
          echo "  for(var j=0; j < product.length; j++)";
          echo "  {";
          echo "    if(product[j][0] == '" . $o_products[$i] . "')";
          echo "    {";
          echo "      orderForm.product" . $j . "_qty.value = '$qty';\n";
          echo "      orderForm.product" . $j . "_colour.value = product[j][1];\n";
          echo "      orderForm.product" . $j . "_size.value = product[j][2];\n";
          echo "      orderForm.product" . $j . "_des.value = product[j][3];\n";
          echo "      orderForm.product" . $j . "_item.value = product[j][4];\n";
          echo "      orderForm.product" . $j . "_total.value = ($qty * product[j][4]).toFixed(2);\n";
          echo "      orderForm.product" . $j . "_qty.disabled = false;\n";
          echo "      break;";
          echo "    }";
          echo "    else if(j == product.length - 1)";
          echo "    {";
          echo "      alert('The product no. " . $o_products[$i] . " does not exist.  Please re-enter the product no.');";
          echo "    }";
          echo "  }";
          $j++;
        }
        echo "  orderForm.freight.value = '" . $_SESSION["o_freight"] . "';";
        echo "  orderForm.merchanise.value = '" . $_SESSION["o_merchanise"] . "';";
        echo "  orderForm.delivery.value = '" . $_SESSION["o_delivery"] . "';";
        echo "  orderForm.insurance.value = '" . $_SESSION["o_insurance"] . "';";
        echo "  orderForm.total.value = '" . $_SESSION["o_total"] . "';";
        if($_SESSION["o_payment"] == "preferred")
          echo "  orderForm.payment[0].checked = true;";
        else if($_SESSION["o_payment"] == "alternate")
          echo "  orderForm.payment[1].checked = true;";
        else
        {
          $card = $_SESSION["o_card"];
          echo "  orderForm.payment[2].checked = true;";
          echo "  orderForm.card_type.value = '" . $card[0] . "';";
          echo "  orderForm.card_name.value = '" . $card[1] . "';";
          echo "  orderForm.card_no_1.value = '" . $card[2] . "';";
          echo "  orderForm.card_no_2.value = '" . $card[3] . "';";
          echo "  orderForm.card_no_3.value = '" . $card[4] . "';";
          echo "  orderForm.card_no_4.value = '" . $card[5] . "';";
          echo "  orderForm.card_expiry_month.value = '" . $card[6] . "';";
          echo "  orderForm.card_expiry_year.value = '20" . $card[7] . "';";
        }
        echo "</SCRIPT>";
      }
      else if(isset($_SESSION["orderProducts"]))
      {
        $products = $_SESSION["orderProducts"];

        echo "<SCRIPT language='Javascript'>\n";
        for($i=0; $i < sizeof($products); $i++)
        {
          echo "orderForm.product" . $i . ".value='" . $products[$i] . "';\n";
          echo "  for(var j=0; j < product.length; j++)";
          echo "  {";
          echo "    if(product[j][0] == '" . $products[$i] . "')";
          echo "    {";
          echo "      orderForm.product" . $i . "_qty.value = '1';\n";
          echo "      orderForm.product" . $i . "_colour.value = product[j][1];\n";
          echo "      orderForm.product" . $i . "_size.value = product[j][2];\n";
          echo "      orderForm.product" . $i . "_des.value = product[j][3];\n";
          echo "      orderForm.product" . $i . "_item.value = product[j][4];\n";
          echo "      orderForm.product" . $i . "_total.value = product[j][4];\n";
          echo "      orderForm.product" . $i . "_qty.disabled = false;\n";
          echo "      break;";
          echo "    }";
          echo "    else if(j == product.length - 1)";
          echo "    {";
          echo "      alert('The product no. " . $products[$i] . " does not exist.  Please re-enter the product no.');";
          echo "    }";
          echo "  }";
        }
        echo "  getMerchanisePrice();";
        echo "</SCRIPT>\n";

      }
    }

    include "$footer";
  }
  else
  {
    session_destroy();
    echo "<SCRIPT language='Javascript'>";
    echo "  parent.window.location.href = 'index.php';";
    echo "</SCRIPT>";
  }

  function showForm()
  {
  
?>

<p align="left">
<b>Date:</b> <?php echo date("Y-m-d"); ?>
</p>

<FORM name="orderForm" action="" method="post">
<TABLE class="order_products" cellpadding="2" cellspacing="2">
  <TR>
    <TD class="order_header" style="width:80px">
      Product No
    </TD>
    <TD class="order_header" style="width:60px">
      Quantity
    </TD>
    <TD class="order_header" style="width:80px">
      Colour
    </TD>
    <TD class="order_header" style="width:50px">
      Size
    </TD>
    <TD class="order_header" style="width:150px">
      Description
    </TD>
    <TD class="order_header" style="width:80px">
      Item Price $
      <br /><small>incl. GST</small>
    </TD>
    <TD class="order_header" style="width:80px">
      Total Price $
      <br /><small>incl. GST</small>
    </TD>
  </TR>

<?php

    for($i=0; $i<10; $i++)
    {

?>

  <TR>
    <TD class="order_enterDetails">
      <input type="text" class="order_products" name="product<?php echo $i; ?>" style="width:80px" onChange="getDetails(this.name, product, detailsSize); getMerchanisePrice()" />
    </TD>
    <TD class="order_enterDetails">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_qty" style="width:60px" onChange="getTotalPrice(this.name, this.value, product, detailsSize); getMerchanisePrice()" />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_colour" style="width:90px" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_size" style="width:50px" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_des" style="width:150px" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_item" style="width:70px" readonly />
    </TD>
    <TD class="order_details">
      <input type="text" class="order_products" name="product<?php echo $i; ?>_total" style="width:70px" readonly />
    </TD>
  </TR>

<?php

    }

?>

</TABLE>

<SCRIPT language='Javascript'>
  orderForm.product0_qty.disabled = true;
  orderForm.product1_qty.disabled = true;
  orderForm.product2_qty.disabled = true;
  orderForm.product3_qty.disabled = true;
  orderForm.product4_qty.disabled = true;
  orderForm.product5_qty.disabled = true;
  orderForm.product6_qty.disabled = true;
  orderForm.product7_qty.disabled = true;
  orderForm.product8_qty.disabled = true;
  orderForm.product9_qty.disabled = true;
</SCRIPT>

<p>

<TABLE class="order_otherDetails">
  <TR>
    <TD valign="top" class="order_odHeader">
      FREIGHT CHARGES
      <br />
      <select class="order_od" name="freight" onChange="getDeliveryPrice()">
        <option value="0">Please select a delivery option: </option>
        <option value="post">Post</option>
        <option value="road">Road</option>
        <option value="air">Air</option>
      </select>
    </TD>
    <TD valign="top" align="right">
      <TABLE border="0" cellpadding="5">
        <TR>
          <TD class="order_odHeader" valign="top" align="right">
            Merchanise Total: $
          </TD>
          <TD class="order_odEnterDetails" valign="top">
            <input type="text" name="merchanise" class="order_odDetails" readonly />
          </TD>
        </TR>
        <TR>
          <TD class="order_odHeader" valign="top" align="right">
            Delivery Charge: $
          </TD>
          <TD class="order_odEnterDetails" valign="top">
            <input type="text" name="delivery" class="order_odDetails" readonly />
          </TD>
        </TR>
        <TR>
          <TD class="order_odHeader" valign="top" align="right">
            Insurance: $
            <br />
            <small>@ $1.00 per $100 value</small>
          </TD>
          <TD class="order_odEnterDetails" valign="top">
            <input type="text" name="insurance" class="order_odDetails" readonly />
          </TD>
        </TR>
        <TR>
          <TD class="order_odHeader" valign="top" align="right">
            TOTAL: $
          </TD>
          <TD class="order_odEnterDetails" valign="top">
            <input type="text" name="total" class="order_odDetails" style="font-weight:bold;" readonly />
          </TD>
        </TR>
      </TABLE>
    </TD>
  </TR>
</TABLE>

<SCRIPT language='Javascript'>
  orderForm.merchanise.value = '0.00';
  orderForm.delivery.value = '0.00';
  orderForm.insurance.value = '0.00';
  orderForm.total.value = '0.00';
</SCRIPT>

<p>

<TABLE class="order_products" cellpadding="2" cellspacing="2">
  <TR>
    <TD class="order_header">
      Payment Details
    </TD>
  </TR>
  <TR>
    <TD>
      <TABLE border="0" cellpadding="2" cellspacing="2">
        <TR>
          <TD class="order_odEnterDetails2" align="right" valign="top" style="width:250px">
            Use current preferred credit card details
          </TD>
          <TD class="order_odEnterDetails2" valign="top" style="width:30px">
            <input type="radio" class="radio" name="payment" value="preferred" checked />
          </TD>
          <TD class="order_odEnterDetails2" style="width:330px">
            &nbsp;
          </TD>
        </TR>
        <TR>
          <TD class="order_odEnterDetails2" align="right" valign="top">
            Use current alternate credit card details
          </TD>
          <TD class="order_odEnterDetails2" valign="top">
            <input type="radio" class="radio" name="payment" value="alternate" />
          </TD>
          <TD class="order_odEnterDetails2">
            &nbsp;
          </TD>
        </TR>
        <TR>
          <TD class="order_odEnterDetails2" align="right" valign="top">
            Use other credit card details
          </TD>
          <TD class="order_odEnterDetails2" valign="top">
            <input type="radio" class="radio" name="payment" value="other" />
          </TD>
          <TD class="order_odEnterDetails2">
            <TABLE>
              <TR>
                <TD>
                  Card Type:
                </TD>
                <TD>
                  <select name="card_type" style="width:100px">
                    <option value="0">Select:</option>
                    <option value="Mastercard">Mastercard</option>
                    <option value="Visa">Visa</option>
                    <option value="Amex">Amex</option>
                    <option value="Diners Club">Diners Club</option>
                  </select>
                </TD>
              </TR>
              <TR>
                <TD>
                  Card Name:
                </TD>
                <TD>
                  <input type="text" class="rcdCardName" name="card_name" style="width:150px">
                </TD>
              </TR>
              <TR>
                <TD>
                  Card Number:
                </TD>
                <TD>
                  <input type="text" class="rcdCardNumber" name="card_no_1" style="width:40px;">
                  <input type="text" class="rcdCardNumber" name="card_no_2" style="width:40px;">
                  <input type="text" class="rcdCardNumber" name="card_no_3" style="width:40px;">
                  <input type="text" class="rcdCardNumber" name="card_no_4" style="width:40px;">
                </TD>
              </TR>
              <TR>
                <TD>
                  Expiry Date:
                </TD>
                <TD>
                  <SCRIPT>
                    fill_select_ex(document.customerForm, "card_expiry_month");
                    year_install_ex(document.customerForm, "card_expiry_year");
                  </script>
                </TD>
              </TR>
            </TABLE>
          </TD>
        </TR>
      </TABLE>
    </TD>
  </TR>
</TABLE>


 <?php

  if($_SESSION["o_alternate"] == false)
  {

?>

<SCRIPT language='Javascript'>
  orderForm.payment[1].disabled = true;
</SCRIPT>

<?php

  }
 
 ?>

<p>

<input class="submit_off" type="submit" value="Submit" name="submit" onmouseover="className='submit_on'" onmouseout="className='submit_off'" />

<p>
&nbsp;
</p>

</FORM>

</BODY>

<?php

  }
  
?>